package highTech.classloader;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class MyClassLoader extends ClassLoader{
	private static final String destPath="lib\\MyClassLoaderAttachment.class";
	private static final String srcPath="E:\\ksfzhaohui\\osp\\workspace\\JavaHighTech\\bin\\classloader\\MyClassLoaderAttachment.class";
	
	public static void main(String[] args)throws Exception {
		InputStream ips=new FileInputStream(srcPath);
		OutputStream ops=new FileOutputStream(destPath);
		cypher(ips, ops);
		
		ips.close();
		ops.close();
	}
	
	private static void cypher(InputStream ips,OutputStream ops) throws Exception{
		int b=-1;
		while((b=ips.read())!=-1){
			//ops.write(b ^ 0xff);
			ops.write(b);
		}
	}
	
	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		try {
			System.out.println("ccccccccc"+name);
			InputStream ips=new FileInputStream("lib\\MyClassLoaderAttachment.class");
			ByteArrayOutputStream ops=new ByteArrayOutputStream();
			cypher(ips, ops);
			ips.close();
			ops.close();
			System.out.println("ads");
			byte[] bytes=ops.toByteArray();
			return defineClass(bytes, 0, bytes.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
